<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of WidgetScoring
 *
 * @author bonfiglio
 */
class WidgetScoring {

    private $db;
    //celui passe par l'url, ou pas
    public $month;
    //Le mois courant
    public $monthCurrent;
    //Convertisseur
    //labels
    private $labels = array(
        'weightAvgCostName' => 'Coût moyen pondéré',
        'avgVolumeName' => 'Volume moyen (en %s)',
        'yAxisTitle1' => '%s HT / %s Cumac',
        'yAxisTitle2' => 'Quantité en %s Cumac',
        'title' => 'Cotation cumac',
        'moneyUnit' => '%s Cumac'
    );
    public $volumeUnit = '';
    public $volumeUnitArg = 1;//Coefficient multiplicateur
    public $volumeUnitList = array(
        1=>'GWh',
        1000=>'MWh'
    );
    public $moneyUnit = '';
    //Les deux listes suivantes doivent faire exactement la même taille, avec les mêmes clé
    public $moneyUnitArg = 1; //Coefficient multiplicateur
    public $moneyUnitList = array(
        1 => 'Cents Euros',
        10 => 'Euros'
    );
    public $costCumacUnitArg = 1; //Coefficient multiplicateur
    public $costCumacUnitList = array(
        1 => 'KWh',
        10 => 'MWh'
    );

    public function __construct($get) {
        $this->db = Database::factory('front');

        //On détermine le mois courant
        $this->monthCurrent = (int) strftime('%m');
        $this->month = isset($get['month']) ? filter_var($get['month'], FILTER_VALIDATE_INT) : $this->monthCurrent;
        $this->month = ($this->month > $this->monthCurrent || $this->month < 0) ? $this->monthCurrent : $this->month;
        //
        $moneyUnitKey = isset($get['moneyUnit']) ? filter_var($get['moneyUnit'], FILTER_SANITIZE_NUMBER_INT) : $this->moneyUnitArg;
        $this->moneyUnitArg = isset($this->moneyUnitList[$moneyUnitKey]) ? $moneyUnitKey : $this->moneyUnitArg;
        $this->costCumacUnitArg = isset($this->moneyUnitList[$moneyUnitKey]) ? $moneyUnitKey : $this->costCumacUnitArg;

        $this->moneyUnit = $this->moneyUnitList[$moneyUnitKey];

        //
        $volumeUnitKey = isset($get['volumeUnit']) ? filter_var($get['volumeUnit'], FILTER_SANITIZE_NUMBER_INT) : $this->volumeUnitArg;
        $this->volumeUnitArg = isset($this->volumeUnitList[$volumeUnitKey]) ? $volumeUnitKey : $this->volumeUnitArg;
        $this->volumeUnit = $this->volumeUnitList[$volumeUnitKey];
    }

    //Labels
    public function getCurrentScoringLabel($name) {
        $return = null;
        $name = (string) $name;
        if (isset($this->labels[$name])) {
            switch ($name) {
                case 'avgVolumeName':
                    $return = sprintf($this->labels['avgVolumeName'], $this->volumeUnitList[$this->volumeUnitArg]);
                    break;
                case 'moneyUnit':
                    $return = sprintf($this->labels['moneyUnit'], $this->moneyUnit);
                    break;
                case 'yAxisTitle1':
                    $return = sprintf($this->labels['yAxisTitle1'], $this->moneyUnit, $this->costCumacUnitList[$this->costCumacUnitArg]);
                    break;
                case 'yAxisTitle2':
                    $return = sprintf($this->labels['yAxisTitle2'], $this->volumeUnit);
                    break;
                case 'weightAvgCostName':
                default:
                    $return = $this->labels[$name];
                    break;
            }
        } elseif ($name = 'volumeUnit') {
            $return = $this->volumeUnit;
        }
        return $return;
    }

    public function getAvgVolumeName() {
        return;
    }

    //Fin Labels


    public function getCurrentMonthSelectOption() {
        $option = '';
        $i = $this->monthCurrent - 1;
        while ($i > -1) {
            $j = (int) strftime('%m', strtotime('-' . $i . ' month'));
            $s = ucfirst(strftime('%B', strtotime('-' . $i . ' month')));
            $selected = ($j == ($this->month)) ? ' selected ' : '';
            $option .= '<option value="' . $j . '" ' . $selected . '>' . $s . ' ' . (int) strftime('%Y') . '</option>';
            $i = $i - 1;
        }
        return $option;
    }

    public function getMoneyUnitOption() {
        $option = '';
        foreach ($this->moneyUnitList as $unit => $value) {
            $selected = ($unit == ($this->moneyUnitArg)) ? ' selected ' : '';
            $option .= '<option value="' . $unit . '" ' . $selected . '>' . $value . ' / ' . $this->costCumacUnitList[$unit] . '</option>';
        }
        return $option;
    }

    public function getVolumeUnitOption() {
        $option = '';
        foreach ($this->volumeUnitList as $unit => $value) {
            $selected = ($unit == $this->volumeUnitArg) ? ' selected ' : '';
            $option .= '<option value="' . $unit . '" ' . $selected . '>' . $value . '</option>';
        }
        return $option;
    }

    public function getCurrentStat() {
//        
        $sql = '
SELECT 
    GROUP_CONCAT(ROUND(average_volume*:volumeUnitArg,3) ORDER BY month SEPARATOR ",") as avgVolume, 
    GROUP_CONCAT(ROUND(weighted_average_cost*:moneyUnitArg,3) ORDER BY month SEPARATOR ",")  as weightAvgCost,
    date,
    year
FROM 
    front.scoring 
WHERE 
    `date` = (SELECT max(`date`) FROM front.scoring) ;
';
        $Result = new Result($this->db->Execute($sql,array(':volumeUnitArg'=>$this->volumeUnitArg, ':moneyUnitArg'=>$this->moneyUnitArg)));
        $datas = $Result->fetch();
        return $datas;
    }

    public function getMonthlyHistoryScoring() {
        $sql = '
                    SELECT 
                    CONCAT_WS(",",(UNIX_TIMESTAMP(date)+(2*3600))*1000, ROUND(weighted_average_cost*:moneyUnitArg,3) ) as data_weighted_average_cost,
                    CONCAT_WS(",",(UNIX_TIMESTAMP(date)+(2*3600))*1000, ROUND(average_volume*:volumeUnitArg,3)) as data_average_volume
                    FROM front.scoring
                    WHERE month = :month;
                ';//
        return new Result($this->db->Execute($sql, array(':volumeUnitArg'=>$this->volumeUnitArg, ':month'=>$this->month,':moneyUnitArg'=>$this->moneyUnitArg)));
    }

    public function getBridgeMonthlyHistoryScoring() {
        $data_weighted_average_cost = $data_average_volume = array();
        $Result = $this->getMonthlyHistoryScoring();
        $d = array();
        while ($v = $Result->Fetch()) {
            $d[] = $v;
            $data_weighted_average_cost[] = '[' . $v['data_weighted_average_cost'] . ']';
            $data_average_volume[] = '[' . $v['data_average_volume'] . ']';
        }
//        Log::pre($d, $this->moneyUnitArg);
        $data_weighted_average_cost = implode(',', $data_weighted_average_cost);
        $data_average_volume = implode(',', $data_average_volume);
        return array($data_weighted_average_cost, $data_average_volume);
    }

}

?>
